//
//  UIView+STTools_Shadow.m
//  STNewTools
//
//  Created by stoneobs on 17/1/12.
//  Copyright © 2017年 stoneobs. All rights reserved.
//
#define STRGB(v)  [UIColor colorWithRed:((float)((v & 0xFF0000) >> 16))/255.0 green:((float)((v & 0xFF00) >> 8))/255.0  blue:((float)(v & 0xFF))/255.0 alpha:1]
#define ST_STONEOBS_UI_FIRST_TEXT_COLOR       STRGB(0x333333)
#define ST_STONEOBS_UI_SECEND_TEXT_COLOR      STRGB(0x666666)
#define ST_STONEOBS_UI_THIRD_TEXT_COLOR       STRGB(0x999999)
#define ST_lineColor            STRGB(0xe0e4e5)

#import "UIView+STTools_Shadow.h"

@implementation UIView (STTools_Shadow)
- (void)stoneobs_fun_st_showTopShadow
{
    self.layer.shadowOffset = CGSizeMake(0, -0.8);
    self.layer.shadowColor = [UIColor grayColor].CGColor;
    self.layer.shadowRadius = 1;
    self.layer.shadowOpacity = .5f;
    CGRect shadowFrame = self.layer.bounds;
    CGPathRef shadowPath = [UIBezierPath
                            bezierPathWithRect:shadowFrame].CGPath;
    self.layer.shadowPath = shadowPath;
}
- (void)stoneobs_fun_st_showBottomShadow
{
    self.layer.shadowOffset = CGSizeMake(0, 0.8);
    self.layer.shadowColor = [UIColor grayColor].CGColor;
    self.layer.shadowRadius = 1;
    self.layer.shadowOpacity = .5f;
    CGRect shadowFrame = self.layer.bounds;
    CGPathRef shadowPath = [UIBezierPath
                            bezierPathWithRect:shadowFrame].CGPath;
    self.layer.shadowPath = shadowPath;
}
- (void)stoneobs_fun_st_showLeftShadow
{
    self.layer.shadowOffset = CGSizeMake(-1, 0);
    self.layer.shadowColor = [UIColor grayColor].CGColor;
    self.layer.shadowRadius = 1;
    self.layer.shadowOpacity = .5f;
    CGRect shadowFrame = self.layer.bounds;
    CGPathRef shadowPath = [UIBezierPath
                            bezierPathWithRect:shadowFrame].CGPath;
    self.layer.shadowPath = shadowPath;
}
- (void)stoneobs_fun_st_showRightShadow
{
    self.layer.shadowOffset = CGSizeMake(1, 0);
    self.layer.shadowColor = [UIColor grayColor].CGColor;
    self.layer.shadowRadius = 1;
    self.layer.shadowOpacity = .5f;
    CGRect shadowFrame = self.layer.bounds;
    CGPathRef shadowPath = [UIBezierPath
                            bezierPathWithRect:shadowFrame].CGPath;
    self.layer.shadowPath = shadowPath;
}
- (void)stoneobs_fun_st_showRightAndBottomShadow
{
    self.layer.shadowOffset = CGSizeMake(1.5, 1.5);
    self.layer.shadowColor = [UIColor grayColor].CGColor;
    self.layer.shadowRadius = 1;
    self.layer.shadowOpacity = 1.0;
    CGRect shadowFrame = self.layer.bounds;
    CGPathRef shadowPath = [UIBezierPath
                            bezierPathWithRect:shadowFrame].CGPath;
    self.layer.shadowPath = shadowPath;
}
//展示周围的layer 到父view
- (CALayer*)stoneobs_fun_st_showRoundLayer{
    CALayer *stoneobs_temp_layer = [CALayer layer];
    stoneobs_temp_layer.frame = self.frame;
    stoneobs_temp_layer.backgroundColor = [UIColor clearColor].CGColor;
    stoneobs_temp_layer.shadowColor =  [UIColor.grayColor colorWithAlphaComponent:0.3].CGColor;
    stoneobs_temp_layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, self.frame.size.width + 5, self.frame.size.height + 5) cornerRadius:self.layer.cornerRadius].CGPath;
    stoneobs_temp_layer.shadowOpacity = 0.5;
    stoneobs_temp_layer.shadowOffset = CGSizeMake(-3, -2);
    stoneobs_temp_layer.cornerRadius = self.layer.cornerRadius;
    //这里self表示当前自定义的view
    [self.superview.layer insertSublayer:stoneobs_temp_layer below:self.layer];
    return stoneobs_temp_layer;
}
- (CALayer*)stoneobs_fun_st_showRoundLayerWithColor:(UIColor*)color{
    CALayer *stoneobs_temp_layer = [CALayer layer];
    stoneobs_temp_layer.frame = self.frame;
    stoneobs_temp_layer.backgroundColor = [UIColor clearColor].CGColor;
    stoneobs_temp_layer.shadowColor =  color.CGColor;
    stoneobs_temp_layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, self.frame.size.width + 5, self.frame.size.height + 5) cornerRadius:self.layer.cornerRadius].CGPath;
    stoneobs_temp_layer.shadowOpacity = 0.5;
    stoneobs_temp_layer.shadowOffset = CGSizeMake(-3, -2);
    stoneobs_temp_layer.cornerRadius = self.layer.cornerRadius;
    //这里self表示当前自定义的view
    [self.superview.layer insertSublayer:stoneobs_temp_layer below:self.layer];
    return stoneobs_temp_layer;
}
//展示左右上下线
- (void)stoneobs_fun_st_showRightLine:(CGFloat)height
{
    UIView * stoneobs_temp_line = [[UIView alloc] initWithFrame:CGRectMake(self.frame.size.width-1, 1, 0.8, self.frame.size.height-2)];
    stoneobs_temp_line.backgroundColor = ST_lineColor;
    [self addSubview:stoneobs_temp_line];
    if (height) {
        CGRect stoneobs_temp_frame = stoneobs_temp_line.frame;
        stoneobs_temp_frame.size.height = height;
        stoneobs_temp_line.frame = stoneobs_temp_frame;
        stoneobs_temp_line.center = CGPointMake(stoneobs_temp_line.center.x, self.frame.size.height/2);
    }
}
- (void)stoneobs_fun_st_showRightInsetLine:(CGFloat)height insetx:(CGFloat)insetx{
    UIView * stoneobs_temp_line = [[UIView alloc] initWithFrame:CGRectMake(self.frame.size.width-1, 1, 0.8, self.frame.size.height-insetx)];
    stoneobs_temp_line.backgroundColor = ST_lineColor;
    [self addSubview:stoneobs_temp_line];
    if (height) {
        CGRect stoneobs_temp_frame = stoneobs_temp_line.frame;
        stoneobs_temp_frame.size.height = height;
        stoneobs_temp_line.frame = stoneobs_temp_frame;
        stoneobs_temp_line.center = CGPointMake(stoneobs_temp_line.center.x, self.frame.size.height/2);
    }
}
- (void)stoneobs_fun_st_showLeftLine:(CGFloat)height
{
    UIView * stoneobs_temp_line = [[UIView alloc] initWithFrame:CGRectMake(1, 1, 0.8, self.frame.size.height - 2)];
    stoneobs_temp_line.backgroundColor = ST_lineColor;
    [self addSubview:stoneobs_temp_line];
    if (height) {
        stoneobs_temp_line.centerY = self.height / 2;
    }
}

- (void)stoneobs_fun_st_showBottomLine
{
    UIView * stoneobs_temp_line = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height-0.5, self.frame.size.width , 0.5)];
    stoneobs_temp_line.backgroundColor = ST_lineColor;
    [self addSubview:stoneobs_temp_line];
    if ([self isKindOfClass:[UIScrollView class]]) {
        UIScrollView * scroll = (UIScrollView*)self;
        stoneobs_temp_line.frame = CGRectMake(0, self.frame.size.height-1, scroll.contentSize.width , 1);
    }
}
- (void)stoneobs_fun_st_showBottomLineAndWitdh:(CGFloat)witdh{
    UIView * stoneobs_temp_line = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height-0.5, witdh , 0.5)];
    stoneobs_temp_line.backgroundColor = ST_lineColor;
    CGPoint stoneobs_temp_center = self.center;
    stoneobs_temp_center.x = self.frame.size.width / 2;
    stoneobs_temp_center.y = self.frame.size.height - 0.5;
    stoneobs_temp_line.center = stoneobs_temp_center;
    [self addSubview:stoneobs_temp_line];
    if ([self isKindOfClass:[UIScrollView class]]) {
        UIScrollView * stoneobs_temp_scroll = (UIScrollView*)self;
        stoneobs_temp_line.frame = CGRectMake(0, self.frame.size.height-1, stoneobs_temp_scroll.contentSize.width , 1);
    }
}
- (void)stoneobs_fun_st_showTopLine
{
    UIView * stoneobs_temp_line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width , 0.5)];
    stoneobs_temp_line.backgroundColor = ST_lineColor;
    [self addSubview:stoneobs_temp_line];
    if ([self isKindOfClass:[UIScrollView class]]) {
        UIScrollView * stoneobs_temp_scroll = (UIScrollView*)self;
        stoneobs_temp_line.frame = CGRectMake(1, self.frame.size.height-1, stoneobs_temp_scroll.contentSize.width - 2, 1);
    }
}
//边框 一般0.3 黑色
- (void)stoneobs_fun_st_setBrderWidth:(CGFloat)width borderColor:(UIColor*)borderColor
{
    self.layer.borderColor = borderColor.CGColor;
    self.layer.borderWidth = width;
}
//展示虚线边框
- (void)stoneobs_fun_st_setDottedLineBrderWidth:(CGFloat)width
                       borderColor:(UIColor*)borderColor{
    
    for (CALayer *stoneobs_temp_layer in self.layer.sublayers) {
        if ([stoneobs_temp_layer isKindOfClass:CAShapeLayer.class]) {
            [stoneobs_temp_layer removeFromSuperlayer];
        }
    }
    
    CAShapeLayer *stoneobs_temp_border = [CAShapeLayer layer];
    //虚线的颜色
    stoneobs_temp_border.strokeColor = borderColor.CGColor;
    //填充的颜色
    stoneobs_temp_border.fillColor = [UIColor clearColor].CGColor;
    //设置路径
    stoneobs_temp_border.path = [UIBezierPath bezierPathWithRect:self.bounds].CGPath;
    stoneobs_temp_border.frame = self.bounds;
    //虚线的宽度
    stoneobs_temp_border.lineWidth = width;
    //设置线条的样式 //
    stoneobs_temp_border.lineCap = @"square";
    //虚线的间隔
    stoneobs_temp_border.lineDashPattern = @[@4, @2];
    [self.layer addSublayer:stoneobs_temp_border];

}
- (void)stoneobs_fun_st_setBorderWith:(CGFloat)width borderColor:(UIColor *)borderColor cornerRadius:(CGFloat)radius{
    self.layer.borderColor = borderColor.CGColor;
    self.layer.borderWidth = width;
    self.layer.cornerRadius = radius;
    self.clipsToBounds = YES;
}
@end
